package com.beemans.corekit.ui.fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.anythink.core.api.ATAdInfo;
import com.beemans.corekit.constant.AdvertConstants;
import com.beemans.corekit.databinding.FragmentToponBannerBinding;
import com.beemans.corekit.integration.toolkit.ToastUtils;
import com.beemans.corekit.integration.topon.ATAdInfoExt;
import com.beemans.corekit.integration.topon.BannerAdvert;
import com.beemans.corekit.integration.topon.BannerAdvertCallback;
import com.beemans.corekit.integration.topon.BannerAdvertConfig;
import com.beemans.corekit.ui.dialog.LoadingDialog;
import com.beemans.corekit.utils.TopOnUtils;

public class TopOnBannerFragment extends BaseFragment<FragmentToponBannerBinding> {
    @Nullable
    @Override
    protected FragmentToponBannerBinding getViewBinding(@NonNull LayoutInflater inflater, @Nullable ViewGroup container) {
        return FragmentToponBannerBinding.inflate(inflater, container, false);
    }

    @Override
    protected void initViews() {
    }

    @Override
    protected void initEvents() {
        binding.bannerBtnLoadAd.setOnClickListener(v -> showAdvert());
    }

    private void showAdvert() {
        LoadingDialog.showLoading();

        final BannerAdvertConfig advertConfig = new BannerAdvertConfig(AdvertConstants.TP_BANNER_ID, 320 / 50f);

        TopOnUtils.loadBannerAdvert(this, binding.bannerFlAd, advertConfig, new BannerAdvertCallback() {
            @Override
            public void onAdvertRequestPre(@NonNull BannerAdvert bannerAdvert, @NonNull ATAdInfoExt adInfoExt) {
                printLog("广告开始请求");
            }

            @Override
            public void onAdvertRequestAlt(@NonNull BannerAdvert bannerAdvert, @NonNull ATAdInfoExt adInfoExt) {
                printLog("广告请求成功");
            }

            @Override
            public void onAdvertLoaded(@NonNull BannerAdvert bannerAdvert) {
                printLog("广告加载成功");
                LoadingDialog.hideLoading();
            }

            @Override
            public void onAdvertLoadFail(@NonNull BannerAdvert bannerAdvert, @Nullable String adError) {
                printLog("广告加载失败");
                LoadingDialog.hideLoading();
            }

            @Override
            public void onAdvertShow(@NonNull BannerAdvert bannerAdvert, @Nullable ATAdInfo atAdInfo) {
                printLog("广告展示 --- atAdInfo:" + atAdInfo);
            }

            @Override
            public void onAdvertShowFail(@NonNull BannerAdvert bannerAdvert) {
                printLog("广告展示失败");
            }

            @Override
            public void onAdvertClicked(@NonNull BannerAdvert bannerAdvert, @Nullable ATAdInfo atAdInfo) {
                printLog("广告点击 --- atAdInfo:" + atAdInfo);
            }

            @Override
            public void onAdvertClose(@NonNull BannerAdvert bannerAdvert, @Nullable ATAdInfo atAdInfo) {
                printLog("广告关闭 --- atAdInfo:" + atAdInfo);
            }

            @Override
            public void onAdvertRevenue(@NonNull BannerAdvert bannerAdvert, @Nullable ATAdInfo atAdInfo) {
                printLog("广告收益 --- atAdInfo:" + atAdInfo);
            }
        });
    }

    private void printLog(String msg) {
        Log.e("susu", msg);
        ToastUtils.showShort(msg);
    }
}